<template>
  <div class="f-container" style="background-color: #20A0FF;">
    <div class="padding50"></div>

    <el-card class="box-card registerBox" style="background-color: white">
      <div class="div-center">
        <h1 class="label-center">注 册 周 报 !</h1>
        <!--<h2 class="label-center">注 册</h2>-->
      </div>
      <br/>
      <el-form :model="form" :rules="rules" ref="registerForm" label-width="70px" class="registerForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" ></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别" >
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" >
          </el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" ></el-input>
        </el-form-item>
        <el-form-item label="密码"  prop="pwd">
          <el-input v-model="form.pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item label="重复"  prop="confirmPwd">
          <el-input v-model="form.confirmPwd" type="password"></el-input>
        </el-form-item>
        <el-form-item label="年级"  prop="grade">
          <el-select v-model="form.grade" placeholder="请选择年级">
            <el-option label="本科" value="本科"></el-option>
            <el-option label="研一" value="研一"></el-option>
            <el-option label="研二" value="研二"></el-option>
            <el-option label="研三" value="研三"></el-option>
            <el-option label="博士" value="博士"></el-option>
            <el-option label="教师" value="教师"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <br/>

      <div class="div-center">
        <el-button type="primary" id="registerButton" size="large" @click="handleRegister">注册</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
  import {Table, TableColumn, Button, Form, FormItem, Dialog, Input, Col, Row, Select, Option, Card} from 'element-ui'
  import * as api_user from '../../api/user';

  export default {
    props: {},
    components: {
      'el-table': Table,
      'el-table-column': TableColumn,
      'el-button': Button,
      'el-form': Form,
      'el-form-item': FormItem,
      'el-dialog': Dialog,
      'el-input': Input,
      'el-row': Row,
      'el-col': Col,
      'el-option': Option,
      'el-card': Card,
      'el-select': Select
    },
    data() {
      let ruleOfConfirmPwd = (rule, value, callback) => {
        if (value !== this.form.pwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        form: {
          name: '',
          sex: '',
          email: '',
          username: '',
          pwd: '',
          confirmPwd: '',
          grade: '',
        },
        rules: {
          name:[
            {required: true, message: '请输入姓名', trigger: 'change'},
          ],
          username: [
            {required: true, message: '请输入用户名', trigger: 'change'},
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'change'},
            {type: 'email', message: '请输入正确的邮箱地址', trigger: 'change'}
          ],
          sex: [
            {required: true, message: '请选择性别', trigger: 'change'},
          ],
          grade: [
            {required: true, message: '请选择年级', trigger: 'change'},
          ],
          pwd: [
            {required: true, message: '请输入密码', trigger: 'change'},
            {min: 6, message: '最小长度为6', trigger: 'blur'}
          ],
          confirmPwd: [
            {required: true, message: '请确认密码', trigger: 'change'},
            {validator: ruleOfConfirmPwd, trigger: 'change,blur'}
          ]
        }
      }
    },
    methods: {

      sendRegisterRequest(){
        api_user.create(this.form).then(() => {
          this.$notify.success({
            title: '注册成功',
            message: '可以登陆了！'
          });
          this.$router.push({name: 'login'});
        }).catch((err) => {
          this.$notify.error({
            title: '注册失败',
            message: err.errmsg + '！'
          });
        })
      },
      handleRegister() {
        this.$refs['registerForm'].validate((valid) => {
          if (valid) {
            console.log(' ⛅ ok');
            this.sendRegisterRequest();
          } else {
            this.$notify.error({
              title: '错 误 ！',
              message: '请重新检查'
            })
          }
        });
      }
    },
    created() {

    },
    mounted() {

    }
  }
</script>


<style scoped>

  .registerBox {
    margin: 0 auto;
    width: 500px;
    color: #20A0FF;
    padding: 20px 30px;
    border-radius: 10px;
  }

  #registerButton {
    margin-top: 10px;
    width: 60%;
  }

  .f-container {
  }

  .el-select {
    width: 100%;
  }

</style>
